﻿Public Class FrmClientes

    Public lista As List(Of EntitiesLayer.Usuario)

    Private Sub FrmClientes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lista = objGestorUsuario.ObtenerUsuarios

        tblClientes.AutoGenerateColumns = False
        tblClientes.DataSource = lista
        tblClientes.Columns("Id").DataPropertyName = "Id"
        tblClientes.Columns("Identificacion").DataPropertyName = "Identificacion"
        tblClientes.Columns("Nombre").DataPropertyName = "PrimerNombre"
        tblClientes.Columns("AliasCliente").DataPropertyName = "Alias"
        tblClientes.Columns("FechaIngreso").DataPropertyName = "FechaIngreso"
        tblClientes.Columns("Estado").DataPropertyName = "Estado"
    End Sub

    Private Sub txtBuscar_TextChanged(sender As Object, e As EventArgs) Handles txtBuscar.TextChanged
        Dim listaFiltrada = New List(Of EntitiesLayer.Usuario)()
        Dim filtro = txtBuscar.Text.ToUpper()
        If filtro Is String.Empty Then
            listaFiltrada = lista
        Else
            For Each usuario As EntitiesLayer.Usuario In lista
                If usuario.Identificacion.ToUpper().Contains(filtro) Or usuario.PrimerNombre.ToUpper().Contains(filtro) Or
                    usuario.Alias.ToUpper().Contains(filtro) Or usuario.FechaIngreso.Contains(filtro) Then
                    listaFiltrada.Add(usuario)
                End If
            Next
        End If
        tblClientes.DataSource = listaFiltrada
    End Sub

    Private Sub btnAgregar_Click(sender As Object, e As EventArgs) Handles btnAgregar.Click
        FrmPrincipal.AddOwnedForm(frmTransparente)
        Dim frm As New FrmRegistrarCliente()
        frm.Dock = DockStyle.Fill
        frmTransparente.AgregarContenido(frm)
        frmTransparente.Show()
    End Sub

    Private Sub btnPago_Click(sender As Object, e As EventArgs)
        Dim idUsuario As Int32 = tblClientes.SelectedCells(tblClientes.Columns("Id").Index).FormattedValue.ToString()
        For Each cliente As EntitiesLayer.Usuario In lista
            If cliente.Id = idUsuario Then
                Me.Controls.Clear()
                Dim frm As New FrmRegistrarPagos
                frm.setUsusario(cliente)
                frm.Dock = DockStyle.Fill
                Me.Controls.Add(frm)
            End If
        Next
    End Sub

    'Private Sub btnInfo_Click(sender As Object, e As EventArgs) Handles btnInfo.Click
    '    MessageBox.Show(tblClientes.SelectedCells(tblClientes.Columns("Id").Index).FormattedValue.ToString())
    '    Dim idUsuario As Int32 = tblClientes.SelectedCells(tblClientes.Columns("Id").Index).FormattedValue.ToString()
    '    For Each cliente As EntitiesLayer.Usuario In lista
    '        If cliente.Id = idUsuario Then
    '            Me.Controls.Clear()
    '            Dim frm As New FrmModificarUsuario()
    '            frm.setCliente(cliente)
    '            frm.Dock = DockStyle.Fill
    '            Me.Controls.Add(frm)

    '        End If
    '    Next
    'End Sub

    Private Sub btnReporte_Click(sender As Object, e As EventArgs) Handles btnReporte.Click
        FrmPrincipal.AddOwnedForm(frmTransparente)
        Dim frm As New FrmReporteMorosos()
        frm.Dock = DockStyle.Fill
        frmTransparente.AgregarContenido(frm)
        frmTransparente.Show()
    End Sub
End Class
